#ifndef COMMON_MANIFEST_H_
#define COMMON_MANIFEST_H_
#include <fermat/common/meta_object.h>
#include <map>
#include <typeinfo>
#include <string>
#include <memory>

namespace fermat {


class  ManifestBase {
public:
	ManifestBase();
	virtual ~ManifestBase();

	virtual const char* className() const = 0;
};


template <class B>
class Manifest: public ManifestBase {
public:
	typedef AbstractMetaObject<B> Meta;
	typedef std::shared_ptr<AbstractMetaObject<B> > MetaPtr;
	typedef std::map<std::string, MetaPtr> MetaMap;

	class Iterator {
	public:
		Iterator(const typename MetaMap::const_iterator& it)
		{
			_it = it;
		}
		Iterator(const Iterator& it)
		{
			_it = it._it;
		}
		~Iterator()
		{
		}
		Iterator& operator = (const Iterator& it)
		{
			_it = it._it;
			return *this;
		}
		inline bool operator == (const Iterator& it) const
		{
			return _it == it._it;
		}
		inline bool operator != (const Iterator& it) const
		{
			return _it != it._it;
		}
		Iterator& operator ++ () // prefix
		{
			++_it;
			return *this;
		}
		Iterator operator ++ (int) // postfix
		{
			Iterator result(_it);
			++_it;
			return result;
		}
		inline const MetaPtr operator * () const
		{
			return _it->second;
		}
		inline const MetaPtr operator -> () const
		{
			return _it->second;
		}
		
	private:
		typename MetaMap::const_iterator _it;
	};

	Manifest()
	{
	}

	virtual ~Manifest()
	{
		clear();
	}

	Iterator find(const std::string& rClassName) const
	{
		return Iterator(_metaMap.find(rClassName));
	}

	Iterator begin() const
	{
		return Iterator(_metaMap.begin());
	}

	Iterator end() const
	{
		return Iterator(_metaMap.end());
	}

	bool insert(const MetaPtr pMeta)
	{
		return _metaMap.insert(typename MetaMap::value_type(pMeta->name(), pMeta)).second;
	}

	void clear()
	{
		_metaMap.clear();
	}

	int size() const
	{
		return int(_metaMap.size());
	}

	bool empty() const
	{
		return _metaMap.empty();
	}

	const char* className() const
	{
		return typeid(*this).name();
	}

private:
	MetaMap _metaMap;
};


} 


#endif 